package com.study.springsecurity.security.user;

import com.study.springsecurity.dao.UserDao;
import com.study.springsecurity.model.User;
import jakarta.annotation.Resource;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zzy
 * @Date: 2024/10/14 23:26
 * @Title:
 * @Version:
 * @Package: com.study.springsecurity.security
 */
@Component
public class DBUserDetailsManager implements UserDetailsService {
    @Resource
    private UserDao userDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userDao.findByMobile(username);
        if (user == null) {
            throw new UsernameNotFoundException(username);
        }
        List<String> authorities = new ArrayList<>();
        authorities.add("author");
        return new MyUserDetail(user,authorities,null);
    }
}
